/*******************************************************************************
*   			Impacts of a Large-Scale Parenting Program: 				   *
*					Experimental Evidence from Chile						   *
********************************************************************************


	REQUIRES:	"${dt_final}panel_RR_indexes.dta"
	CREATES:	${out_tables}/T18A_leebounds.xls
				
	WRITEN BY:  Italo Lopez  [italolop@usc.edu]

********************************************************************************
	Prepare data
*******************************************************************************/

	* Load data
	use "${dt_final}/panel_RR_indexes.dta", clear


	* Establish macros for regressions

/*******************************************************************************
*   Set macros for regressions
*******************************************************************************/
	
	* Outcomes
	*-----------------------------------------------------------
	local childoutcomes  "zDCCSPunt zptevir_irtscore2 zt_score zcbcl_t_e zcbcl_t_i zibatt_sspi zibatt_ssai zibatt_sssr zas_raw_score" 
	local behaviors "home_score se_score disc_score" 
	local beliefs "zpstyle1 zpstyle2 zpstyle3 zPSCS zPSSS_family zPSSS_friends zPSSS_others zPSI_Distress zCESD zpacotis"	
	local child "zptevir_irtscore2 zcog_index zsoc_index" 

	* Controls
	*-----------------------------------------------------------

	macro  def Xvar1 "i.age_year i.gender"  	
	macro  def Xvar2 "i.age_year i.gender inc_qtaut_old i.pc_edu_mdsfin hh_mem hh_tipo"
  	macro  def Xvar3 "i.age_year i.gender inc_qtaut_old t_score_old dCCSPunt_old hh_mem hh_tipo"

	
	* Parental characteristics: WAIS and BFI and imputed wais 
	*-----------------------------------------------------------

	macro def parent_cha4 "BFI_ext BFI_ope BFI_con BFI_neu BFI_agr wais2 flag_wais"
	

/*******************************************************************************
Table A18: Lee Bounds (z-index)
*******************************************************************************/
	//ta TIPO_old, g(tipoold)

	//g nonatt=(1-attrite) 
	//g female_old=sexo_old==2


	* Using raw outcomes

	local outcomes "zptevir_irtscore2 zcog_index zsoc_index "
	foreach var of local outcomes {
		 xi: xtreg `var' tipoold2 $Xvar3 if inlist(TIPO_old,1,2), fe i(CENTRO_SALUD_old)  /* EXCLUDE EDUCATION AND INCLUDE BASELINE LANGUAGE */
		predict `var'_resB if e(sample), res
		g `var'_restipo2=_b[tipoold2]*tipoold2 + `var'_resB

		xi: xtreg `var' tipoold3 $Xvar3 if inlist(TIPO_old,1,3), fe i(CENTRO_SALUD_old)
		predict `var'_resI if e(sample), res
		g `var'_restipo3=_b[tipoold3]*tipoold3 + `var'_resI
	}


	** Table A18 **
	local outcomes "zptevir_irtscore2 zcog_index zsoc_index"
	foreach var of local outcomes {
		xi: leebounds `var'_restipo2 tipoold2 if inlist(TIPO_old,1,2), sel(nonatt) cieffect
		estimates store `var'_restipo2
		xi: leebounds `var'_restipo3 tipoold3 if inlist(TIPO_old,1,3), sel(nonatt) cieffect
		estimates store `var'_restipo3
		

	}
	
	
	esttab zptevir_irtscore2_restipo2 zptevir_irtscore2_restipo3  /*
	*/ using "${out_tables}/appendix/T18A_leebounds.csv", /*
	*/ replace f cells("b(fmt(3)star)" "se(fmt(3)par)") /*
	*/ star(* 0.10 ** 0.05 *** 0.01) /*
	*/ title("Vocubalary Index") /*
	*/ mtitles("NEP-B" "NEP-I"  ) 


	esttab   zcog_index_restipo2 zcog_index_restipo3   /*
	*/ using "${out_tables}/appendix/T18A_leebounds.csv", /*
	*/ append f cells("b(fmt(3)star)" "se(fmt(3)par)") /*
	*/ star(* 0.10 ** 0.05 *** 0.01)  /*
	*/ title("Executive Function Index") /*
	*/ mtitles("NEP-B" "NEP-I"  ) 
	
	esttab  zsoc_index_restipo2 zsoc_index_restipo3 /*
	*/ using "${out_tables}/appendix/T18A_leebounds.csv", /*
	*/ append f cells("b(fmt(3)star)" "se(fmt(3)par)") /*
	*/ star(* 0.10 ** 0.05 *** 0.01)  /*
	*/ title("Socioemotional Index") /*
	*/ mtitles("NEP-B" "NEP-I"  ) 
	